import DBtil.DBtil1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo03_Insert {
    public static void main(String[] args) {
        //定义连接对象
        Connection connection=null;
        //定义处理SQL执行对象
        PreparedStatement preparedStatement=null;//可解决sql注入问题
        //定义结果集对象
        //ResultSet resultSet=null;//不需要，insert返回的是受影响的行数
        //1.获取数据库连接
        try {
            connection = DBtil1.getConnection();
            //2.定义SQL
            String sql="insert into student(student_id,sn,name) values(?,?,?)";
            //3.定义预处理对象
            preparedStatement=connection.prepareStatement(sql);

            System.out.println("请输入学生编号：");
            Scanner scanner=new Scanner(System.in);
            Long ID=Long.valueOf(scanner.next());
            System.out.println("请输入学生邮箱：");
            String mail=scanner.next();
            System.out.println("请输入学生姓名：");
            String name=scanner.next();
            //4.用真实数据填充占位符
            preparedStatement.setLong(1,ID);
            preparedStatement.setString(2,mail);
            preparedStatement.setString(3,name);
            //5.执行SQL获取结果
            int row=preparedStatement.executeUpdate();
            //6.判断结果
            if(row==1) {
                System.out.println("插入成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBtil1.close(null,preparedStatement,connection);
        }

    }
}
